package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entiy.Check;
import com.jsoft.entiy.Contract;
import com.jsoft.entiy.vo.ContractInfoVo;
import com.jsoft.entiy.vo.ContractVo;
import com.jsoft.service.ContractService;
import com.jsoft.service.impl.ContractServiceImpl;
import com.jsoft.util.DelSpace;
import com.jsoft.util.ResultUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@WebServlet("*.Contract")
public class ContractController extends HttpServlet {
    private ContractService contractService = new ContractServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        //1.去掉第一个/
        String methodName = servletPath.substring(1);
        //截取addVip
        methodName = methodName.substring(methodName.lastIndexOf("/") + 1, methodName.length() - 9);
        System.out.println(methodName);
        try {
            //拿到当前类要执行的方法对象
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //获取所有合同
    private void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Contract> all = contractService.getAll();
        for (Contract contract : all) {
            System.out.println(contract);
        }
    }

    //获取未付款部分合同信息带分页
    private void getUnpaidInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String nowPage = req.getParameter("nowPage");
        PrintWriter out = resp.getWriter();
        List<ContractVo> info = contractService.getUnpaidContractInfo(nowPage);
        Integer totalNum = contractService.getUnpaidContractInfo();
        System.out.println(info);
        out.write(JSON.toJSONString(new ResultUtil(totalNum, info)));
    }

    //获取已付款部分合同信息
    private void getPaidInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String nowPage = req.getParameter("nowPage");
        PrintWriter out = resp.getWriter();
        List<ContractVo> info = contractService.getPaidContractInfo(nowPage);
        Integer totalNum = contractService.getPaidContractInfo();
        System.out.println(info);
        out.write(JSON.toJSONString(new ResultUtil(totalNum, info)));
    }

    //获取续费的部分合同信息
    private void getRenewalInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String nowPage = req.getParameter("nowPage");
        PrintWriter out = resp.getWriter();
        List<ContractVo> info = contractService.getRenewalContractInfo(nowPage);
        Integer totalNum = contractService.getRenewalContractInfo();
        System.out.println(info);
        out.write(JSON.toJSONString(new ResultUtil(totalNum, info)));
    }

    //根据id获取合同信息
    private void getInfoById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Integer id = Integer.parseInt(req.getParameter("id"));
        ContractInfoVo infoById = contractService.getInfoById(id);
        System.out.println(infoById);
        out.write(JSON.toJSONString(new ResultUtil("info", infoById)));

    }

    //获取随机编号
    private void getNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String word = "1234567890";
        StringBuilder strb = new StringBuilder();

        for (int i = 0; i < 6; i++) {
            strb.append(word.charAt((int) Math.floor(Math.random() * word.length())));
        }
        out.write(JSON.toJSONString(new ResultUtil("info", strb.toString())));

    }

    //根据公司名获取公司信息
    private void getCompanyInfoByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String company = req.getParameter("company");
        System.out.println(company);
        String s = DelSpace.delSpace(company);
        System.out.println(s);
        ContractInfoVo companyInfo = contractService.getInfoVoByName(s);
        System.out.println(companyInfo);
        if (companyInfo == null) {
            out.write(JSON.toJSONString(new ResultUtil("0", companyInfo)));
        }
        out.write(JSON.toJSONString(new ResultUtil("1", companyInfo)));
    }

    //添加公司信息
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        PrintWriter out = resp.getWriter();
        String json = reader.readLine();
        Contract contract = JSON.parseObject(json, Contract.class);

        Integer save = contractService.save(contract);
        if (save > 0) {
            out.write(JSON.toJSONString(new ResultUtil("1", "添加成功")));
        } else {
            out.write(JSON.toJSONString(new ResultUtil("0", "添加失败")));
        }


    }

    //条件查询
    private void checkInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
//        BufferedReader reader = req.getReader();
        PrintWriter out = resp.getWriter();
//        String json = reader.readLine();
//        Check check = JSON.parseObject(json, Check.class);
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String nowPage = req.getParameter("nowPage");

        Check check = new Check(name, type, startTime, endTime);
        System.out.println(check);
        System.out.println(nowPage);
        List<ContractVo> contractVos = contractService.checkInfo(check,nowPage);
        System.out.println(contractVos);
        Integer totalNum = contractService.checkInfo(check);
        if (Objects.nonNull(contractVos)) {
            out.write(JSON.toJSONString(new ResultUtil(totalNum, contractVos)));
        } else {
            out.write(JSON.toJSONString(new ResultUtil("0", "没有查询到")));
        }

    }

    //根据id删除合同信息
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        //        BufferedReader reader = req.getReader();
        String id = req.getParameter("id");
        PrintWriter out = resp.getWriter();
        Integer delete = contractService.deleteById(Integer.parseInt(id));
        if (delete > 0) {
            out.write(JSON.toJSONString(new ResultUtil("1", "删除成功")));
        } else {
            out.write(JSON.toJSONString(new ResultUtil("0", "删除失败")));
        }
    }

    //根据id修改合同状态
    private void updateContractById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        //        BufferedReader reader = req.getReader();
        String id = req.getParameter("id");
        String paystate = req.getParameter("paystate");
        PrintWriter out = resp.getWriter();
        Integer update= contractService.updateById(Integer.parseInt(id),paystate);
        if (update > 0) {
            out.write(JSON.toJSONString(new ResultUtil("1", "更改成功")));
        } else {
            out.write(JSON.toJSONString(new ResultUtil("0", "更改失败")));
        }
    }

}
